define(
["./StaticRenderSyst", "framework/graphic/WebGL"],
function(StaticRenderSyst, WebGL){
    return {
        create: function() {
            var _system = null;
            var _visible = true;
            var _lineDrawOffset = 0;
            var _lineDrawLen = 0;
            var _triDrawOffset = 0;
            var _triDrawLen = 0;

            var _self = {
                //The buffers
                posBuf: null,
                norBuf: null,
                colorBuf: null,
                indicesBuf: null,
                visibleBuf: null,
                lineIndicesBuf: null,

                init: function(systemMgr, posBuf, norBuf, colorBuf, indicesBuf, visibleBuf, lineIndicesBuf) {
                    //Get and register myself to the system
                    _system = systemMgr.getSystem(StaticRenderSyst);
                    _system.add(_self);

                    //Assign buffers
                    _self.posBuf = posBuf;
                    _self.norBuf = norBuf;
                    _self.colorBuf = colorBuf;
                    _self.indicesBuf = indicesBuf;
                    _self.visibleBuf = visibleBuf;
                    _self.lineIndicesBuf = lineIndicesBuf;

                    //Reset drawing configuration
                    _self.toDefaultConf();

                    return this;
                },

                destroy: function() {
                    //Unregister myself from the system
                    _system.rem(_self);
                },

                /**
                 * Set the drawing configuration to default (draw all)
                 */
                toDefaultConf: function() {
                    _self.setTriDrawConf(0, _self.indicesBuf.numItems);
                    _self.setLineDrawConf(0, _self.lineIndicesBuf.numItems);
                    _self.setVisible(true);
                },

                /**
                 * Call this to draw to the screen
                 */
                render: function(shader) {
                    //Check visible
                    if(!_visible) return;

                    //Assign the buffers
                    WebGL.pointAttrib(shader.getAttribute("aPos"), _self.posBuf);
                    WebGL.pointAttrib(shader.getAttribute("aNor"), _self.norBuf);
                    WebGL.pointAttrib(shader.getAttribute("aColor"), _self.colorBuf);
                    WebGL.pointAttrib(shader.getAttribute("aVisible"), _self.visibleBuf);

                    //Draw call
                    var gl = WebGL.gl;

                    //Polygon offset
                    gl.enable(gl.POLYGON_OFFSET_FILL);
                    gl.polygonOffset(1, 1);

                    //Draw front
                    WebGL.gl.uniform1i(shader.getUniform("uuseColor"), 1);
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _self.indicesBuf);
                    gl.drawElements(gl.TRIANGLES, _triDrawLen, gl.UNSIGNED_SHORT, _triDrawOffset);
                    
                    //Polygon offset
                    gl.disable(gl.POLYGON_OFFSET_FILL);

                    //Draw back
                    WebGL.gl.uniform1i(shader.getUniform("uuseColor"), 0);
                    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, _self.lineIndicesBuf);
                    gl.drawElements(gl.LINES, _lineDrawLen, gl.UNSIGNED_SHORT, _lineDrawOffset);
                },

                setVisible: function(val) {
                    _visible = val;
                },

                setLineDrawConf: function(start, count) {
                    if(start)
                        _lineDrawOffset = start;
                    if(count)
                        _lineDrawLen = count;
                },

                setTriDrawConf: function(start, count) {
                    if(start)
                        _triDrawOffset = start;
                    if(count)
                        _triDrawLen = count;
                },
            };

            return _self;
        }
    }
});